package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.dto.SupervisorVo;
import com.prd.mapper.EntTaskCollectMapper;
import com.prd.model.EntTaskCollect;
import com.prd.model.LoginUser;
import com.prd.service.EntTaskCollectService;
import com.prd.util.UserInfoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EntTaskCollectServiceImpl extends ServiceImpl<EntTaskCollectMapper, EntTaskCollect> implements EntTaskCollectService {

    @Autowired
    private EntTaskCollectMapper entTaskCollectMapper;

    @Override
    public int insertInfo(EntTaskCollect entTaskCollect) {
        LambdaQueryWrapper<EntTaskCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntTaskCollect::getTaskId, entTaskCollect.getTaskId());
        queryWrapper.eq(entTaskCollect.getStageId() != null, EntTaskCollect::getStageId, entTaskCollect.getStageId());
        EntTaskCollect TaskCollect = entTaskCollectMapper.selectOne(queryWrapper);
        if (TaskCollect != null) {
            throw new RuntimeException("该记录已经存在！");
        }
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entTaskCollect);
        return entTaskCollectMapper.insert(entTaskCollect);
    }

    @Override
    public int updateInfo(EntTaskCollect entTaskCollect) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entTaskCollect);
        return entTaskCollectMapper.updateById(entTaskCollect);
    }

    @Override
    public int addSupervisor(SupervisorVo supervisorVo) {
        EntTaskCollect entTaskCollect = entTaskCollectMapper.selectById(supervisorVo.getId());
        if (entTaskCollect == null) {
            throw new RuntimeException("记录没有找到！");
        }
        entTaskCollect.setSupervisorReach(supervisorVo.getReach());
        entTaskCollect.setSupervisorGist(supervisorVo.getGist());
        entTaskCollect.setSupervisorContent(supervisorVo.getContent());
        entTaskCollect.setSupervisorRecord(supervisorVo.getRecord());
        entTaskCollect.setSupervisorPicture(supervisorVo.getPicture());
        return updateInfo(entTaskCollect);
    }
}
